﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DSPL.Contracts;

namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for ImageButton.xaml
    /// </summary>
    public partial class FileBrowseButton : UserControl
    {
        public FileBrowseButton()
        {
            InitializeComponent();
        }
        
        public string FilePath
        {
            get { return (string)GetValue(FilePathProperty); }
            set { SetValue(FilePathProperty, value); }
        }

        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set { SetValue(FileNameProperty, value); }
        }
        
        public FileFilters FileFilter
        {
            get { return (FileFilters)GetValue(FileFilterProperty); }
            set { SetValue(FileFilterProperty, value); }
        }
        
        //public string FileSavedPath
        //{
        //    get { return (string)GetValue(FileSavedPathProperty); }
        //    set { SetValue(FileSavedPathProperty, value); }
        //}

        //public static readonly DependencyProperty FileSavedPathProperty =
        //    DependencyProperty.Register("FileSavedPath", typeof(string), typeof(ImageButton), new PropertyMetadata(0));
        
        public static readonly DependencyProperty FileFilterProperty =
            DependencyProperty.Register("FileFilter", typeof(FileFilters), typeof(FileBrowseButton), new PropertyMetadata(FileFilters.All));
                
        public static readonly DependencyProperty FilePathProperty =
            DependencyProperty.Register("FilePath", typeof(string), typeof(FileBrowseButton), new PropertyMetadata(""));

        public static readonly DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(FileBrowseButton), new PropertyMetadata(""));

        
        private void ClickImageButton(object sender, RoutedEventArgs e)
        {
            OpenFileDialog FileDialog = new OpenFileDialog();
            FileDialog.Title = "Select A File";
            FileDialog.InitialDirectory = "";
            FileDialog.Filter = GetFileFilter();
            FileDialog.FilterIndex = 1;

            if (FileDialog.ShowDialog().Value)
            {             
                FileName = FileDialog.SafeFileName;
                FilePath = FileDialog.FileName;
            }
        }

        private string GetFileFilter()
        {
            string fileFilter = "All|*";

            switch (FileFilter)
            {
                case FileFilters.ImageFiles:
                    fileFilter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png";
                    break;
                case FileFilters.DocFiles:
                    break;
                case FileFilters.ExelFiles:
                    break;
                case FileFilters.PdfFiles:
                    break;
                case FileFilters.All:
                    break;
                default:
                    break;
            }

            return fileFilter;
        }
    }
}
